/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nxlv2.nomad.view.ficheClient;

import com.nxlv2.nomad.accessdb.services.AdresseServices;
import com.nxlv2.nomad.accessdb.services.ClientServices;
import com.nxlv2.nomad.businessobjects.Adresse;
import com.nxlv2.nomad.businessobjects.Client;
import java.awt.Color;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author Nastasia
 */
public class FormulaireAdresse extends javax.swing.JFrame {

    private final FicheClient fc;
    private final Client cl;

    /**
     * Creates new form FormulaireAdresse
     */
    public FormulaireAdresse(FicheClient fc, Client client) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.fc = fc;
        this.cl = client;
        lblError.setText(null);
        lblNomClient.setText(fc.getTxtNomClient().getText());
        if (null != fc.getAdresse()) {
            txtNumVoie.setText(fc.getAdresse().getNumVoie());
            txtNomVoie.setText(fc.getAdresse().getNomVoie());
            txtComplement.setText(fc.getAdresse().getComplement());
            txtCodePostal.setText(fc.getAdresse().getCodePostal());
            txtVille.setText(fc.getAdresse().getVille());
            txtPays.setText(fc.getAdresse().getPays());
            txtMail.setText(fc.getAdresse().getMail());
            txtTel.setText(fc.getAdresse().getTel());
            if (fc.getAdresse().isDefaut()) {
                chekBoxPrincipale.setSelected(true);
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblNomClient = new javax.swing.JLabel();
        lblNumVoie = new javax.swing.JLabel();
        txtNumVoie = new javax.swing.JTextField();
        lblNomVoie = new javax.swing.JLabel();
        txtNomVoie = new javax.swing.JTextField();
        txtComplement = new javax.swing.JTextField();
        lblComplement = new javax.swing.JLabel();
        lblCodePostal = new javax.swing.JLabel();
        txtCodePostal = new javax.swing.JTextField();
        lblVille = new javax.swing.JLabel();
        txtVille = new javax.swing.JTextField();
        lblPays = new javax.swing.JLabel();
        txtPays = new javax.swing.JTextField();
        lblMail = new javax.swing.JLabel();
        txtMail = new javax.swing.JTextField();
        lblTel = new javax.swing.JLabel();
        txtTel = new javax.swing.JTextField();
        btnValider = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        lblError = new javax.swing.JLabel();
        chekBoxPrincipale = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Formulaire des adresses");

        lblNomClient.setText("jLabel1");

        lblNumVoie.setText("N° rue :");

        lblNomVoie.setText("Nom rue* :");

        lblComplement.setText("Complément :");

        lblCodePostal.setText("Code postal* :");

        lblVille.setText("Ville* :");

        lblPays.setText("Pays* :");

        lblMail.setText("Mail* :");

        lblTel.setText("Téléphone* :");

        btnValider.setText("Valider");
        btnValider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnValiderActionPerformed(evt);
            }
        });

        jLabel9.setText("* Champs obligatoires");

        lblError.setForeground(new java.awt.Color(255, 0, 0));
        lblError.setText("jLabel10");

        chekBoxPrincipale.setText("Adresse principale");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblError, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblTel)
                                    .addComponent(lblMail)
                                    .addComponent(lblPays)
                                    .addComponent(lblVille)
                                    .addComponent(lblCodePostal)
                                    .addComponent(lblComplement)
                                    .addComponent(lblNomVoie)
                                    .addComponent(lblNumVoie))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblNomClient, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(txtComplement, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(txtNomVoie, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(txtNumVoie, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(txtVille, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(txtPays, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(txtMail, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(txtTel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnValider, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(txtCodePostal, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(chekBoxPrincipale)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addComponent(jLabel9)))
                        .addGap(0, 127, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblNomClient)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNumVoie)
                    .addComponent(txtNumVoie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNomVoie)
                    .addComponent(txtNomVoie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblComplement)
                    .addComponent(txtComplement, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCodePostal)
                    .addComponent(txtCodePostal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblVille)
                    .addComponent(txtVille, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPays)
                    .addComponent(txtPays, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMail)
                    .addComponent(txtMail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTel)
                    .addComponent(txtTel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chekBoxPrincipale)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblError)
                .addGap(18, 18, 18)
                .addComponent(btnValider)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnValiderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnValiderActionPerformed
        // TODO add your handling code here:
        ClientServices srvCl = new ClientServices();

        Pattern p = Pattern.compile("^[a-zA-Z0-9éèùàêôûâî \\-@]+$");
        Pattern p1 = Pattern.compile("^[a-zA-Z0-9\\.]+[@]{1}[a-zA-Z0-9\\.]+[\\.]{1}[a-zA-Z]+$");
        Pattern p2 = Pattern.compile("^[0-9 \\-\\+\\.]+$");
        Pattern p3 = Pattern.compile("^[a-zA-Z \\-]+$");
        Pattern p4 = Pattern.compile("^[a-zA-Z0-9 \\-@]*$");

        Matcher mNumVoie = p4.matcher(txtNumVoie.getText());
        Matcher mNomVoie = p.matcher(txtNomVoie.getText());
        Matcher mComplement = p4.matcher(txtComplement.getText());
        Matcher mCp = p.matcher(txtCodePostal.getText());
        Matcher mVille = p3.matcher(txtVille.getText());
        Matcher mPays = p3.matcher(txtPays.getText());
        Matcher mMail = p1.matcher(txtMail.getText());
        Matcher mTel = p2.matcher(txtTel.getText());

        int error = 0;
        // Num Voie Adresse
        if (!mNumVoie.matches()) {
            lblNumVoie.setForeground(Color.red);
            error = 1;
        } else {
            lblNumVoie.setForeground(Color.black);
        }
        // Nom Voie Adresse
        if (!mNomVoie.matches() || "".equals(txtNomVoie.getText())) {
            lblNomVoie.setForeground(Color.red);
            error = 1;
        } else {
            lblNomVoie.setForeground(Color.black);
        }
        // Complement Adresse
        if (!mComplement.matches()) {
            lblComplement.setForeground(Color.red);
            error = 1;
        } else {
            lblComplement.setForeground(Color.black);
        }
        // Code Postal Adresse
        if (!mCp.matches() || "".equals(txtCodePostal.getText())) {
            lblCodePostal.setForeground(Color.red);
            error = 1;
        } else {
            lblCodePostal.setForeground(Color.black);
        }
        // Ville Adresse
        if (!mVille.matches() || "".equals(txtVille.getText())) {
            lblVille.setForeground(Color.red);
            error = 1;
        } else {
            lblVille.setForeground(Color.black);
        }
        // Pays Adresse
        if (!mPays.matches() || "".equals(txtPays.getText())) {
            lblPays.setForeground(Color.red);
            error = 1;
        } else {
            lblPays.setForeground(Color.black);
        }
        // Mail Adresse
        if (!mMail.matches() || "".equals(txtMail.getText())) {
            lblMail.setForeground(Color.red);
            error = 1;
        } else {
            lblMail.setForeground(Color.black);
        }
        // Tel Adresse
        if (!mTel.matches() || "".equals(txtTel.getText())) {
            lblTel.setForeground(Color.red);
            error = 1;
        } else {
            lblTel.setForeground(Color.black);
        }

        if (error == 1) {
            lblError.setText("Veuillez corriger ou compléter les champs indiqués");
        } else {
            AdresseServices srvAdr = new AdresseServices();
            lblError.setText(null);
            Adresse adr = new Adresse();
            if (null != fc.getAdresse()) {
                adr = fc.getAdresse();
            }
            adr.setNumVoie(txtNumVoie.getText());
            adr.setNomVoie(txtNomVoie.getText());
            adr.setComplement(txtComplement.getText());
            adr.setCodePostal(txtCodePostal.getText());
            adr.setVille(txtVille.getText());
            adr.setPays(txtPays.getText());
            adr.setMail(txtMail.getText());
            adr.setTel(txtTel.getText());
            if (chekBoxPrincipale.isSelected() == true) {
                System.out.println("cochée");
                List<Adresse> adresses;
                adresses = srvCl.getAdresses(cl);
                for (Adresse adresse : adresses) {
                    if (adresse.isDefaut() == true) {
                        adresse.setDefaut(false);
                        srvAdr.update(adresse);
                    }
                }
                adr.setDefaut(true);
            }
            if (null != fc.getAdresse()) {
                srvAdr.update(adr);
            } else {
                srvAdr.create(adr, cl);
            }
            fc.majAdresses();
            dispose();
        }
    }//GEN-LAST:event_btnValiderActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnValider;
    private javax.swing.JCheckBox chekBoxPrincipale;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblCodePostal;
    private javax.swing.JLabel lblComplement;
    private javax.swing.JLabel lblError;
    private javax.swing.JLabel lblMail;
    private javax.swing.JLabel lblNomClient;
    private javax.swing.JLabel lblNomVoie;
    private javax.swing.JLabel lblNumVoie;
    private javax.swing.JLabel lblPays;
    private javax.swing.JLabel lblTel;
    private javax.swing.JLabel lblVille;
    private javax.swing.JTextField txtCodePostal;
    private javax.swing.JTextField txtComplement;
    private javax.swing.JTextField txtMail;
    private javax.swing.JTextField txtNomVoie;
    private javax.swing.JTextField txtNumVoie;
    private javax.swing.JTextField txtPays;
    private javax.swing.JTextField txtTel;
    private javax.swing.JTextField txtVille;
    // End of variables declaration//GEN-END:variables
}
